local H = require "packages/hegemony/util"
local HM = require "packages/mod_hegemony/util"
local U = require "packages/utility/utility"
local extension = Package:new("reinforce")
extension.extensionName = "mod_hegemony"
extension.game_modes_whitelist = { 'nos_heg_mode', 'new_heg_mode' }
extension:loadSkillSkelsByPath("./packages/mod_hegemony/pkg/reinforce/skills")

Fk:loadTranslationTable{
  ["reinforce"] = "国战-重铸",
  ["re_heg"] = "重铸",
}


local xuyou = General(extension, "re_heg__xuyou", "qun", 3)
xuyou.subkingdom = "wei"
xuyou:addSkill("re_heg__chenglue")
xuyou:addSkill("re_heg__shicai")

Fk:loadTranslationTable{
  ["re_heg__xuyou"] = "许攸",
  ["#re_heg__xuyou"] = "毕方矫翼",
  ["designer:re_heg__xuyou"] = "逍遥鱼叔",
  ["illustrator:re_heg__xuyou"] = "猎枭",

  ["re_heg__chenglue"] = "成略",
  [":re_heg__chenglue"] = "当与你势力相同的角色使用牌指定目标后，若目标数大于1，你可令其摸一张牌。若如此做且你受到过此牌造成的伤害，此牌结算完成后，你可令一名与你势力相同且没有国战标记的角色获得一个“阴阳鱼”标记。",
  ["re_heg__shicai"] = "恃才",
  [":re_heg__shicai"] = "锁定技，当你受到伤害后，若此伤害为1点，你摸一张牌，否则你弃置两张牌。",

  ["#re_heg__chenglue_after"] = "成略",
  ["#re_heg__chenglue-ask"] = "成略：你可令 %src 摸一张牌",
  ["#re_heg__chenglue-give"] = "成略：你可令一名与你势力相同且没有国战标记的角色获得一个“阴阳鱼”标记",

  ["$re_heg__chenglue1"] = "阿瞒苦思之事，我早有良策。",
  ["$re_heg__chenglue2"] = "策略已有，按部就班即可得胜。",
  ["$re_heg__shicai1"] = "如此大胜，皆由我一人谋划。",
  ["$re_heg__shicai2"] = "画谋定计，谁堪与我比较。",
  ["~re_heg__xuyou"] = "阿瞒，你竟忘恩负义！！",
}


local caohong = General(extension, "re_heg__caohong", "wei", 4)
caohong:addSkill("re__heyi")
caohong:addSkill("re_heg__huyuan")
caohong:addCompanions("hs__caoren")
Fk:loadTranslationTable{
  ["re_heg__caohong"] = "曹洪",
  ["#re_heg__caohong"] = "魏之福将",
  ["designer:re_heg__caohong"] = "韩旭（淬毒）",
  ["illustrator:re_heg__caohong"] = "YellowKiss",
  ["cv:re_heg__caohong"] = "绯川陵彦",

  ["re__heyi"] = "鹤翼",
  [":re__heyi"] = "阵法技，你和与你处于同一队列的角色拥有“飞影”。",
  ["re_heg__huyuan"] = "护援",
  [":re_heg__huyuan"] = "结束阶段，你可选择：1.将一张手牌交给一名角色；2.将一张装备牌置入一名角色的装备区，然后弃置场上的一张牌。然后若其体力值不大于你，其回复1点体力。",

  ["huyuan_active"] = "护援",
  ["re_heg__huyuan_give"] = "给出手牌",
  ["re_heg__huyuan_equip"] = "置入装备",

  ["#re_heg__huyuan-choose"] = "发动 护援，选择一张牌和一名角色",
  ["#re_heg__huyuan_discard-choose"] = "护援：选择一名角色，弃置其场上的一张牌",

  ["re_heg__feiying"] = "飞影",
  [":re_heg__feiying"] = "锁定技，其他角色计算与你的距离+1。",

  ["$re_heg__huyuan2"] = "舍命献马，护我曹公！",
  ["$re_heg__huyuan1"] = "拼将性命，也要保将军周全！",
  ["~re_heg__caohong"] = "曹公，可安好...",

}
--[[
local bianfuren = General(extension, "re_heg__bianfuren", "wei", 3)
local wanwei = fk.CreateTriggerSkill{
  name = "re_heg__wanwei",
  events = {fk.AfterCardsMove},
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self) or player:usedSkillTimes(self.name, Player.HistoryRound) ~= 0 then return end
    local cids = {}
    local from = {}
    for _, move in ipairs(data) do
      if move.from and move.moveReason ~= fk.ReasonUse and move.moveReason ~= fk.ReasonResonpse then
        local room = player.room
        from = room:getPlayerById(move.from)
        if H.compareKingdomWith(player, from) then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand then
              table.insertIfNeed(cids, info.cardId)
            end
          end
        end
      end
    end
    if #cids > 0 and not from.dead then
      self.cost_data = {from, cids}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local from = self.cost_data[1]
    local cids = self.cost_data[2]
    local cid = table.random(cids, 1)[1]
    local name = Fk:getCardById(cid).trueName
    if name then
      local cards = room:getCardsFromPileByRule(name, data.damage, "discardPile") or {}
      if #cards > 0 then
        room:obtainCard(from, table.random(cards, 1), false, fk.ReasonJustMove, player.id)
      else
        from:drawCards(1, self.name)
      end
    else
      from:drawCards(1, self.name)
    end
  end,
}

local yuejian = fk.CreateTriggerSkill{
  name = "re_heg__yuejian",
  anim_type = "support",
  frequency = Skill.Compulsory,
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return H.compareKingdomWith(player, target) and player:hasSkill(self) and target.phase == Player.Discard 
      and target:getMark("re_heg__yuejian-turn") == 0 and player.room.current == target
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:doIndicate(player.id, {target.id})
    room:addPlayerMark(target, "_yuejian_maxcard-turn", 1)
  end,

  refresh_events = {fk.TargetSpecified},
  can_refresh = function(self, event, target, player, data)
    return H.compareKingdomWith(target, player) and target == player.room.current
      and target:getMark("re_heg__yuejian-turn") == 0 and data.firstTarget and data.card.type ~= Card.TypeEquip
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    for _, id in ipairs(AimGroup:getAllTargets(data.tos)) do
      if not H.compareKingdomWith(room:getPlayerById(id), target) then
        room:addPlayerMark(target, "re_heg__yuejian-turn", 1)
        break
      end
    end
  end,
}
local yuejian_maxcards = fk.CreateMaxCardsSkill{
  name = "#re_heg__yuejian_maxcards",
  fixed_func = function(self, player)
    if player:getMark("_yuejian_maxcard-turn") > 0 then
      return player.maxHp
    end
  end
}
yuejian:addRelatedSkill(yuejian_maxcards)

bianfuren:addCompanions("hs__caocao")
bianfuren:addSkill(wanwei)
bianfuren:addSkill(yuejian)

Fk:loadTranslationTable{
  ["re_heg__bianfuren"] = "卞夫人",
  ["#re_heg__bianfuren"] = "奕世之雍容",
  ["illustrator:re_heg__bianfuren"] = "雪君S",

  ["re_heg__wanwei"] = "挽危",
  [":re_heg__wanwei"] = "每回合限一次，一名与你势力相同的角色不因使用或打出而失去手牌后，你可以令其随机获得弃牌堆中的一张同名牌，若其未以此法获得牌，其摸一张牌。",
  ["re_heg__yuejian"] = "约俭",
  [":re_heg__yuejian"] = "锁定技，与你势力相同角色的弃牌阶段开始时，若其本回合处于与你势力相同状态时未对其他势力角色使用过牌，其本回合的手牌上限改为其体力上限。",

  ["#re_heg__wanwei-choose"] = "挽危：请选择等量即将被其他角色弃置或获得的牌",

  ["$re_heg__wanwei1"] = "梁、沛之间，非子廉无有今日。",
  ["$re_heg__wanwei2"] = "正使祸至，共死何苦！",
  ["$re_heg__yuejian1"] = "无文绣珠玉，器皆黑漆。",
  ["$re_heg__yuejian2"] = "性情约俭，不尚华丽。",
  ["~re_heg__bianfuren"] = "心肝涂地，惊愕断绝……",
}

local ganfuren = General(extension, "re_heg__ganfuren", "shu", 3, 3, General.Female)
local shushen = fk.CreateTriggerSkill{
  name = "re_heg__shushen",
  anim_type = "support",
  events = {fk.HpRecover},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self)
  end,
  on_trigger = function(self, event, target, player, data)
    self.cancel_cost = false
    for i = 1, data.num do
      if self.cancel_cost then break end
      self:doCost(event, target, player, data)
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local to = room:askForChoosePlayers(player, table.map(room:getOtherPlayers(player),
    Util.IdMapper), 1, 1, "#re_heg__shushen-choose", self.name, true)
    if #to > 0 then
      self.cost_data = to[1]
      return true
    end
    self.cancel_cost = true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(self.cost_data)
    if not to:isKongcheng() then
      to:drawCards(1, self.name)
    else
      to:drawCards(2, self.name)
    end
  end,
}

ganfuren:addCompanions("hs__liubei")
ganfuren:addSkill(shushen)
ganfuren:addSkill("shenzhi")

Fk:loadTranslationTable{
  ['re_heg__ganfuren'] = '甘夫人',
  ["#re_heg__ganfuren"] = "昭烈皇后",
  ["illustrator:re_heg__ganfuren"] = "琛·美弟奇",
  ["designer:re_heg__anfuren"] = "淬毒",

  ["re_heg__shushen"] = "淑慎",
  [":re_heg__shushen"] = "当你回复1点体力后，你可令一名其他角色摸一张牌，若其没有手牌，则改为摸两张牌。",

  ["#re_heg__shushen-choose"] = "淑慎：你可令一名其他角色摸一张牌",

  ["$re_heg__shushen1"] = "船到桥头自然直。",
  ["$re_heg__shushen2"] = "妾身无恙，相公请安心征战。",
  ["~re_heg__ganfuren"] = "请替我照顾好阿斗……",
}

local xushu = General(extension, "re_heg__xushu", "shu", 4)
xushu.deputyMaxHpAdjustedValue = -1
local pozhen = fk.CreateTriggerSkill{
  name = "re_heg__pozhen",
  anim_type = "control",
  frequency = Skill.Limited,
  events = {fk.TurnStart},
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(self) and target ~= player and player:usedSkillTimes(self.name, Player.HistoryGame) == 0
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(target, "@@re_heg__pozhen_prohibit-turn", 1)
    local members = {target}
    local array = H.getFormationRelation(target)
    for _, p in ipairs(array) do
      table.insertIfNeed(members, p)
    end
    local siege = H.getOtherSiegeMembers(target)
    if #siege > 0 then
      for _, p in ipairs(siege) do
        table.insertIfNeed(members, p)
      end
    end
    members = table.map(members, Util.IdMapper)
    room:sortPlayersByAction(members)
    for _, id in ipairs(members) do
      if player.dead then break end
      local cid = room:askForCardChosen(player, room:getPlayerById(id), "he", self.name, "#re_heg__pozhen-choose")
      room:throwCard(cid, self.name, room:getPlayerById(id), player)
    end
  end,
}
local pozhen_prohibit = fk.CreateProhibitSkill{
  name = "#re_heg__pozhen_prohibit",
  prohibit_use = function(self, player, card)
    return player:getMark("@@re_heg__pozhen_prohibit-turn") > 0 and card and table.contains(player:getCardIds("h"), card:getEffectiveId())
  end,
  prohibit_response = function(self, player, card)
    return player:getMark("@@re_heg__pozhen_prohibit-turn") > 0 and card and table.contains(player:getCardIds("h"), card:getEffectiveId())
  end,
}
local jiancai = fk.CreateTriggerSkill{
  name = "re_heg__jiancai",
  anim_type = "defensive",
  relate_to_place = "d",
  events = {fk.DamageInflicted},
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(self) and H.compareKingdomWith(player, data.to) and data.damage >= data.to.hp
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name, nil, "#re_heg__jiancai-ask") 
  end,
  on_use = function (self, event, target, player, data)
    data.damage = 0
    H.transformGeneral(player.room, player)
  end,
  refresh_events = {"fk.GeneralTransforming"},
  can_refresh = function (self, event, target, player, data)
    return player:hasShownSkill(self) and H.compareKingdomWith(target, player)
  end,
  on_refresh = function (self, event, target, player, data)
    data.num = (data.num or 3) + 2
  end
}
pozhen:addRelatedSkill(pozhen_prohibit)
xushu:addSkill("zhuhai")
--xushu:addSkill(pozhen)
xushu:addSkill(jiancai)
xushu:addCompanions("hs__wolong")
Fk:loadTranslationTable{
  ["re_heg__xushu"] = "徐庶",
  ["#re_heg__xushu"] = "化剑为犁",
  ["designer:re_heg__xushu"] = "梦魇狂朝&Loun老萌",
  ["illustrator:re_heg__xushu"] = "YanBai",
  ["re_heg__pozhen"] = "破阵",
  [":re_heg__pozhen"] = "限定技，其他角色的回合开始时，你可以令其本回合不可使用或打出手牌；若其处于队列或围攻关系中，你可依次弃置此队列或参与围攻关系的其他角色的一张牌。",
  ["@@re_heg__pozhen_prohibit-turn"] = "被破阵",
  ["#re_heg__pozhen-choose"] = "破阵：弃置其一张牌",
  ["re_heg__jiancai"] = "荐才",
  [":re_heg__jiancai"] = "副将技，此武将牌上单独的阴阳鱼个数-1。与你势力相同的角色即将受到伤害而进入濒死状态时，你可以防止此伤害，若如此做，你须变更副将；与你势力相同的角色变更副将时，其额外获得两张备选武将牌。",
  ["#re_heg__jiancai-ask"] = "荐才：你可以防止此伤害，然后变更副将",

  ["$re_heg__pozhen1"] = "既遇明主，天下可图！",
  ["$re_heg__pozhen2"] = "弃武从文，安邦卫国！",
  ["$re_heg__jiancai1"] = "开言纳谏，社稷之福。",
  ["$re_heg__jiancai2"] = "如此如此，敌军自破！",
  ["~re_heg__xushu"] = "大义无言，虽死无怨。",
}

local luxun = General(extension, "re_heg__luxun", "wu", 3)

local qianxun = fk.CreateTriggerSkill{
  name = "re_heg__qianxun",
  anim_type = "defensive",
  events = {fk.TargetConfirming, fk.BeforeCardsMove},
  frequency = Skill.Compulsory,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self) then return false end
    if event == fk.TargetConfirming then
      return target == player and player:hasSkill(self) and data.card.name == "snatch"
    elseif event == fk.BeforeCardsMove then
      local id = 0
      local source = player
      local room = player.room
      local c
      for _, move in ipairs(data) do
        if move.to == player.id and move.toArea == Card.PlayerJudge then
          for _, info in ipairs(move.moveInfo) do
            id = info.cardId
            if info.fromArea == Card.PlayerJudge then
              source = room:getPlayerById(move.from) or player
            else
              source = player
            end
            c = source:getVirualEquip(id)
            --FIXME：巨大隐患，延时锦囊的virtual_equips在置入判定区的事件被篡改，或者判定阶段自然流程以外的方式离开判定区时不会清理
            if not c then c = Fk:getCardById(id) end
            if c.trueName == "indulgence" then
              return true
            end
          end
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:notifySkillInvoked(player, self.name)
    if event == fk.TargetConfirming then
      player:broadcastSkillInvoke(self.name, 2)
      AimGroup:cancelTarget(data, player.id)
      return true
    elseif event == fk.BeforeCardsMove then
      player:broadcastSkillInvoke(self.name, 1)
      local source = player
      local mirror_moves = {}
      local ids = {}
      for _, move in ipairs(data) do
        if move.to == player.id and move.toArea == Card.PlayerJudge then
          local move_info = {}
          local mirror_info = {}
          for _, info in ipairs(move.moveInfo) do
            local id = info.cardId
            if info.fromArea == Card.PlayerJudge then
              source = room:getPlayerById(move.from) or player
            else
              source = player
            end
            local c = source:getVirualEquip(id)
            if not c then c = Fk:getCardById(id) end
            if c.trueName == "indulgence" then
              table.insert(mirror_info, info)
              table.insert(ids, id)
            else
              table.insert(move_info, info)
            end
          end
          if #mirror_info > 0 then
            move.moveInfo = move_info
            local mirror_move = table.clone(move)
            mirror_move.to = nil
            mirror_move.toArea = Card.DiscardPile
            mirror_move.moveInfo = mirror_info
            table.insert(mirror_moves, mirror_move)
          end
        end
      end
      table.insertTable(data, mirror_moves)
    end
  end
}

local duoshi = fk.CreateActiveSkill{
  name = "re_heg__duoshi",
  anim_type = "drawcard",
  target_num = 0,
  card_num = 1,
  card_filter = function(self, to_select, selected)
    return #selected == 0 and Fk:getCardById(to_select).color == Card.Red
     and Fk:currentRoom():getCardArea(to_select) == Player.Hand
  end,
  target_filter = Util.FalseFunc,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) < 2
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local targets = table.filter(room.alive_players, function(p)
      return H.compareKingdomWith(player, p)
    end)
    room:throwCard(effect.cards, self.name, player, player)
    for _, p in ipairs(targets) do
      p:drawCards(2, self.name)
    end

    local extraData = {
      num = 2,
      min_num = 2,
      include_equip = true,
      pattern = ".",
      reason = self.name,
    }
    local prompt = "#askForDuoShi"
    local command = "AskForUseActiveSkill"
    local data = { "choose_cards_skill", prompt, false, extraData }

    local req = Request:new(targets, command)
    req.focus_text = "#re_heg__duoshi_considering"

    for _, to in ipairs(targets) do
      req:setData(to, data)
    end

    for _, p in ipairs(targets) do
      local cids 
      if p.reply_ready then
        local replyCard = req:getResult(p).card
        cids = table.random(replyCard.subcards, 2)
      else
        cids = table.random(p:getCardIds(Player.Hand), 2)
      end
      room:throwCard(cids, self.name, p, p)
    end

  end,
}

luxun:addCompanions("ld__lukang")
luxun:addSkill(qianxun)
luxun:addSkill(duoshi)

Fk:loadTranslationTable{
  ["re_heg__luxun"] = "陆逊",
  ["#re_heg__luxun"] = "擎天之柱",
  ["illustrator:re_heg__luxun"] = "KayaK",

  ["re_heg__qianxun"] = "谦逊",
  [":re_heg__qianxun"] = "锁定技，当你成为【顺手牵羊】或【乐不思蜀】的目标时，你取消此目标。",
  ["re_heg__duoshi"] = "度势",
  [":re_heg__duoshi"] = "出牌阶段限两次，你可以弃置一张红色手牌，令与你势力相同的角色各摸两张牌，然后同时选择两张牌并依次弃置这些牌。",
  ["#askForDuoShi"] = "度势：弃置两张牌",
  ["#re_heg__duoshi_considering"] = "度势",
  
  ["$re_heg__qianxun1"] = "儒生脱尘，不为贪逸淫乐之事。",
  ["$re_heg__qianxun2"] = "谦谦君子，不饮盗泉之水。",
  ["$re_heg__duoshi1"] = "以今日之大事，当行此计。",
  ["$re_heg__duoshi2"] = "国之大计，审势为先。",
  ["~re_heg__luxun"] = "还以为我已经不再年轻……",
}

local dingfeng = General(extension, "re_heg__dingfeng", "wu", 4)
local duanbing = fk.CreateTriggerSkill{
  name = "re_heg__duanbing",
  anim_type = "offensive",
  events = {fk.AfterCardTargetDeclared, fk.TargetSpecified},
  can_trigger = function(self, event, target, player, data)
    if event == fk.AfterCardTargetDeclared then
      return target == player and player:hasSkill(self) and data.card.trueName == "slash" and
        table.find(player.room:getUseExtraTargets(data, false), function(id)
          return player:distanceTo(player.room:getPlayerById(id)) == 1
        end)
    elseif (data or {}).card then
      return player:hasSkill(self) and target == player and data.card.trueName == "slash" and player:distanceTo(player.room:getPlayerById(data.to)) <= 1
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if event == fk.AfterCardTargetDeclared then
      local targets = table.filter(room:getUseExtraTargets(data, false), function(id)
        return player:distanceTo(room:getPlayerById(id)) == 1
      end)
      local tos = room:askForChoosePlayers(player, targets, 1, 1, "#re_heg__duanbing-choose", self.name, true)
      if #tos > 0 then
        self.cost_data = tos[1]
        return true
      end
    else
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    if event == fk.AfterCardTargetDeclared then
      table.insert(data.tos, {self.cost_data})
    else
      data.fixedResponseTimes = data.fixedResponseTimes or {}
      data.fixedResponseTimes["jink"] = 2
    end
  end,
}
dingfeng:addCompanions("ld__xusheng")
dingfeng:addSkill(duanbing)
dingfeng:addSkill("fenxun")
Fk:loadTranslationTable{
  ["re_heg__dingfeng"] = "丁奉",
  ["#re_heg__dingfeng"] = "清侧重臣",
  ["illustrator:hs__dingfeng"] = "魔鬼鱼",
  ["re_heg__duanbing"] = "短兵",
  [":re_heg__duanbing"] = "你使用【杀】时可以额外选择一名距离为1的其他角色为目标。你对距离为1的角色使用【杀】后，其需要连续使用两张【闪】来抵消此【杀】。",
  ["#re_heg__duanbing-choose"] = "短兵：你可以额外选择一名距离为1的其他角色为目标",

  ["$re_heg__duanbing1"] = "弃马，亮兵器，杀！",
  ["$re_heg__duanbing2"] = "雪中奋短兵，快者胜！",
  ["$fenxun1"] = "取封侯爵赏，正在今日！",
  ["$fenxun2"] = "给我拉过来！",
  ["~re_heg__dingfeng"] = "这风，太冷了……",
}

local xusheng = General(extension, "re_heg__xusheng", "wu", 4)
local yicheng = fk.CreateTriggerSkill{
  name = "re_heg__yicheng",
  anim_type = "defensive",
  events = {fk.TargetConfirmed, fk.TargetSpecified},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and H.compareKingdomWith(target, player) and data.card.trueName == "slash" and (event == fk.TargetConfirmed or (event == fk.TargetSpecified and data.firstTarget))
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name, nil, "#re_heg__yicheng-ask::" .. target.id)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:doIndicate(player.id, {target.id})
    target:drawCards(1, self.name)
    if not target.dead then
      room:askForDiscard(target, 1, 1, true, self.name, false)
    end
  end
}

xusheng:addSkill(yicheng)
xusheng:addCompanions("hs__dingfeng")
xusheng:addCompanions("re_heg__dingfeng")

Fk:loadTranslationTable{
  ["re_heg__xusheng"] = "徐盛",
  ["#re_heg__xusheng"] = "江东的铁壁",
  ["designer:re_heg__xusheng"] = "淬毒",
  ["illustrator:re_heg__xusheng"] = "天信",
  ["re_heg__yicheng"] = "疑城",
  [":re_heg__yicheng"] = "当与你势力相同的角色使用【杀】指定目标后或成为【杀】的目标后，你可令其摸一张牌，然后其弃置一张牌。",

  ["#re_heg__yicheng-ask"] = "疑城：你可令 %dest 摸一张牌，然后其弃置一张牌",

  ["$re_heg__yicheng1"] = "不怕死，就尽管放马过来！",
  ["$re_heg__yicheng2"] = "待末将布下疑城，以退曹贼。",
  ["~re_heg__xusheng"] = "可怜一身胆略，尽随一抔黄土……",
}

local chengdong = General(extension, "re_heg__chenwudongxi", "wu", 4)
local duanxie = fk.CreateActiveSkill{
  name = 're_heg__duanxie',
  anim_type = 'offensive',
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  card_filter = function() return false end,
  target_filter = function(self, to_select, selected)
    return #selected < 2 and to_select ~= Self.id and
      not Fk:currentRoom():getPlayerById(to_select).chained
      and (#selected == 0 or H.compareKingdomWith(Fk:currentRoom():getPlayerById(to_select), Fk:currentRoom():getPlayerById(selected[1])))
  end,
  min_target_num = 1,
  max_target_num = 2,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local ids = effect.tos
    room:sortPlayersByAction(ids)
    for _, id in ipairs(ids) do
      local target = room:getPlayerById(id)
      if not target.chained then
        target:setChainState(true)
      end
    end

    if not player.chained then
      player:setChainState(true)
    end
  end,
}
local fenming = fk.CreateTriggerSkill{
  name = 're_heg__fenming',
  anim_type = 'control',
  events = { fk.EventPhaseStart, fk.ChainStateChanged },
  can_trigger = function(self, event, target, player, data)
    if event == fk.EventPhaseStart then
      return target == player and player:hasSkill(self) and
        player.phase == Player.Finish and player.chained 
    elseif event == fk.ChainStateChanged then
      return target == player and player:hasSkill(self) and not player.chained
    end
    return
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      for _, p in ipairs(room:getAlivePlayers()) do
        if p.chained and not p:isNude() then
          local c = room:askForCardChosen(player, p, "hej", self.name)
          room:throwCard(c, self.name, p, player)
        end
      end
    else
      player:drawCards(1, self.name)
    end
  end,
}
chengdong:addSkill(duanxie)
chengdong:addSkill(fenming)
Fk:loadTranslationTable{
  ['re_heg__chenwudongxi'] = '陈武董袭',
  ["#re_heg__chenwudongxi"] = "壮怀激烈",
  ["designer:re_heg__chenwudongxi"] = "淬毒",
  ["illustrator:re_heg__chenwudongxi"] = "地狱许",

  ['re_heg__duanxie'] = '断绁',
  [':re_heg__duanxie'] = '出牌阶段限一次，你可以横置同一势力至多两名其他角色，然后若你未横置，则你横置。',
  ['re_heg__fenming'] = '奋命',
  [':re_heg__fenming'] = '结束阶段，若你处于横置状态，你可弃置所有处于横置状态角色区域内的各一张牌。当你重置后，你可以摸一张牌。',

  ["$re_heg__duanxie1"] = "区区绳索就想挡住吾等去路？！",
  ["$re_heg__duanxie2"] = "以身索敌，何惧同伤！",
  ["$re_heg__fenming1"] = "东吴男儿，岂是贪生怕死之辈？",
  ["$re_heg__fenming2"] = "不惜性命，也要保主公周全！",
  ["~re_heg__chenwudongxi"] = "杀身卫主，死而无憾！",
}

local sufei = General(extension, "re_heg__sufei", "qun", 4)
sufei.subkingdom = "wu"
local lianpian = fk.CreateTriggerSkill{
  name = "re_heg__lianpian",
  anim_type = "support",
  events = {fk.EventPhaseStart},
  can_trigger = function (self, event, target, player, data)
    if player == target and player.phase == Player.Finish then
      local room = player.room
      local turn_event = room.logic:getCurrentEvent():findParent(GameEvent.Turn, true)
      local ids = {}
      room.logic:getEventsByRule(GameEvent.MoveCards, 1, function (e)
        for _, move in ipairs(e.data) do
          if move.toArea == Card.DiscardPile and move.moveReason == fk.ReasonDiscard 
          and move.proposer == player.id then
            for _, info in ipairs(move.moveInfo) do
              table.insertIfNeed(ids, info.cardId)
            end
          end
        end
        return false
      end, turn_event.id)
      if player:hasSkill(self) then
        if #ids > player.hp then
          local targets = {}
          for _, p in ipairs(room.alive_players) do
            if H.compareKingdomWith(p, player, false) then
              table.insertIfNeed(targets, p)
            end
          end
          if #targets > 0 then
            self.cost_data = targets
            return true
          end
        end
      else
        local owner = table.filter(room.alive_players, function(p)
          return p:hasSkill(self)
        end) or {}
        if #owner > 0 then
          owner = owner[1]
          if #ids > owner.hp then
            self.cost_data = owner
            return true
          end
        end 
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    if player:hasSkill(self) then
      local targets = self.cost_data
      local to = room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 1, "#jieming-choose", self.name, true)
      if #to > 0 then
        to = room:getPlayerById(to[1])
        local num = to.maxHp - to:getHandcardNum()
        if num > 0 then
          self.cost_data = {to, num}
          return true
        end
      end
    else
      local owner = self.cost_data
      local id = owner.id
      local choices = {}
      if not owner:isNude() then
        table.insert(choices, "lianpian_discard:"..id)
      end
      if owner:isWounded() then
        table.insert(choices, "lianpian_recover:"..id)
      end
      table.insert(choices, "lianpian_cancel")
      local choice = room:askForChoice(player, choices, self.name, "#re_heg__lianpian-choose:", nil, {"lianpian_discard:"..id, "lianpian_recover:"..id, "lianpian_cancel"})
      if choice ~= "lianpian_cancel" then
        self.cost_data = {choice, owner}
        return true
      end
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    if player:hasSkill(self) then
      local to = self.cost_data[1]
      local num = self.cost_data[2]
      to:drawCards(num, self.name)
    else
      local choice = self.cost_data[1]
      local owner = self.cost_data[2]
      if choice:startsWith("lianpian_discard") then
        owner:drawCards(1, self.name)
        --local cid = room:askForCardChosen(player, owner, "he", self.name, "#re_heg__lianpian-discard")
        --room:throwCard(cid, self.name, owner, player)
      elseif choice:startsWith("lianpian_recover") then
        room:recover{
          who = owner,
          num = 1,
          recoverBy = player,
          skillName = self.name
        }
      end
    end
  end,
}
local zhengjian = fk.CreateTriggerSkill{
  name = "re_heg__zhengjian",
  anim_type = "defensive",
  events = {fk.EventPhaseStart},
  can_trigger = function (self, event, target, player, data)
    if not (H.compareKingdomWith(player, target) and target.phase == Player.Finish and player:hasSkill(self)) then return false end
    if target:getMark("@!companion") ~= 0 then return end
    local events = target.room.logic:getEventsOfScope(GameEvent.UseCard, 999, function(e)
      local use = e.data[1]
      return use.from == target.id
    end, Player.HistoryTurn)
    return #events >= target.maxHp
  end,
  on_use = function (self, event, target, player, data)
    H.addHegMark(player.room, target, "companion")
    player.room:setPlayerMark(target, "@@re_heg__zhengjian-forbidden", 1)
  end,

  refresh_events = {fk.TargetConfirmed, fk.BuryVictim, fk.EnterDying},
  can_refresh = function (self, event, target, player, data)
    if event == fk.TargetConfirmed then
      return player:hasSkill(self) and player == target and data.card.trueName == "slash"
    elseif event == fk.BuryVictim then
      return player == target and player:hasSkill(self)
    elseif event == fk.EnterDying then
      return player:hasSkill(self) and target:getMark("@@re_heg__zhengjian-forbidden") > 0
    end
  end,
  on_refresh = function (self, event, target, player, data)
    if event == fk.TargetConfirmed or event == fk.BuryVictim then
      local targets = table.filter(player.room.alive_players, function(p) return p:getMark("@@re_heg__zhengjian-forbidden") > 0 end)
      if #targets > 0 then
        for _, p in ipairs(targets) do
          player.room:setPlayerMark(p, "@@re_heg__zhengjian-forbidden", 0)
        end
      end
    else
      player.room:setPlayerMark(target, "@@re_heg__zhengjian-forbidden", 0)
    end
  end,
}

local zhengjian_prohibit = fk.CreateProhibitSkill{
  name = "#zhengjian_prohibit",
  prohibit_use = function(self, player, card)
    return card.trueName == "peach" and player:getMark("@@re_heg__zhengjian-forbidden") > 0
  end,
}

sufei:addCompanions("hs__ganning")
zhengjian:addRelatedSkill(zhengjian_prohibit)
sufei:addSkill(lianpian)
Fk:loadTranslationTable{
  ["re_heg__sufei"] = "苏飞",
  ["#re_heg__sufei"] = "诤友投明",
  -- ["designer:re_heg__sufei"] = "逍遥鱼叔",
  ["illustrator:re_heg__sufei"] = "Domi",

  ["re_heg__zhengjian"] = "诤荐",
  [":re_heg__zhengjian"] = "与你势力相同角色的结束阶段，若其本回合使用牌数不小于其体力上限且没有“珠联璧合”标记，你可令其获得一个“珠联璧合”标记，若如此做，其不能使用【桃】直至你成为【杀】的目标或其进入濒死状态。",

  ["@@re_heg__zhengjian-forbidden"] = "诤荐 禁用桃",

  ["re_heg__lianpian"] = "联翩",
  [":re_heg__lianpian"] = "结束阶段，若你于此回合内弃置任意角色牌的总和大于你的体力值，你可以令一名与你势力相同的角色将手牌摸至体力上限。其他角色的结束阶段，"..
  "若其于此回合内弃置任意角色牌的总和大于你的体力值，其可以弃置你的一张牌或令你回复1点体力。",
  ["#re_heg__lianpian-choose"] = "",
  ["#re_heg__lianpian-discard"] = "",
  ["lianpian_discard"] = "弃置%src一张牌",
  ["lianpian_recover"] = "令%src回复1点体力",
  ["lianpian_cancel"] = "取消",

  ["$re_heg__lianpian1"] = "需持续投入，方有回报。",
  ["$re_heg__lianpian2"] = "心无旁骛，断而敢行。",

  ["~re_heg__sufei"] = "恐不能再与兴霸兄，并肩作战了……",
}

local lvbu = General(extension, "re_heg__lvbu", "qun", 5)
local yongdou = fk.CreateTriggerSkill{
  name = "re_heg__yongdou",
  anim_type = "offensive",
  events = {fk.CardUsing},
  can_trigger = function(self, event, target, player, data)
    if target == player and data.card and player:hasSkill(self) then
      if data.card.name == "duel" then
        if data.card:isVirtual() then
          return #data.card.subcards == 0
        else
          return true
        end
      end
    end
    return
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = room:getUseExtraTargets(data, true, false)
    local tos = room:askForChoosePlayers(player, targets, 1, 2, "#re_heg__yongdou-choose", self.name, true, false)
    if #tos > 0 then
      self.cost_data = tos
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local tos = self.cost_data
    for _, id in ipairs(tos) do
      table.insert(data.tos, {id})
    end
  end,
}
lvbu:addSkill("wushuang")
lvbu:addSkill(yongdou)
lvbu:addCompanions("hs__diaochan")

Fk:loadTranslationTable{
  ["re_heg__lvbu"] = "吕布",
  ["#re_heg__lvbu"] = "戟指中原",
  ["illustrator:re_heg__lvbu"] = "凡果",
  ["re_heg__yongdou"] = "勇斗",
  [":re_heg__yongdou"] = "你使用非转化的【决斗】时，你可以额外指定至多两个目标。",
  ["#re_heg__yongdou-choose"] = "勇斗：你可以为此【决斗】指定至多两个额外目标",
  ["~re_heg__lvbu"] = "不可能！",
}

--并非重铸
local liufuren = General(extension, "re_heg__liufuren", "qun", 3, 3, General.Female)
local zhuidu = fk.CreateTriggerSkill{
  name = "re_heg__zhuidu",
  anim_type = "offensive",
  events = {fk.DamageCaused},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target == player and player:usedSkillTimes(self.name, Player.HistoryTurn) == 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = data.to
    local isBeishui = false
    if data.to.gender == General.Female then
      local c = room:askForDiscard(player, 1, 1, true, self.name, true, ".", "#re_heg__zhuidu-discard")
      if #c > 0 then
        isBeishui = true
      end
    end
    if not isBeishui and #table.filter(to:getCardIds("e"), function(id)
      return not to:prohibitDiscard(Fk:getCardById(id))
    end) > 0 then
      local choice = Sea.askForChoice(to, {"zhuidu_add_damage", "zhuidu_discard"}, self.name, "#re_heg__zhuidu-choice", false)
      if choice == "zhuidu_add_damage" then
        data.damage = data.damage + 1
        return
      else
        room:throwCard(table.filter(to:getCardIds("e"), function(id)
          return not to:prohibitDiscard(Fk:getCardById(id))
        end), self.name, to, to)
      end
    elseif not isBeishui and #table.filter(to:getCardIds("e"), function(id)
      return not to:prohibitDiscard(Fk:getCardById(id))
    end) == 0 then
      data.damage = data.damage + 1
      return
    else
      data.damage = data.damage + 1
      room:throwCard(table.filter(to:getCardIds("e"), function(id)
        return not to:prohibitDiscard(Fk:getCardById(id))
      end), self.name, to, to)
      return
    end
  end,
}
local shigong = fk.CreateTriggerSkill{
  name = "re_heg__shigong",
  anim_type = "support",
  frequency = Skill.Limited,
  events = {fk.EnterDying},
  can_trigger = function(self, event, target, player, data)
    return player == target and player:hasSkill(self) and player.phase == Player.NotActive and player:usedSkillTimes(self.name, Player.HistoryGame) == 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local current = room.current
    local deputy_general = player.deputyGeneral
    local skills = Fk.generals[deputy_general]:getSkillNameList()
    skills = table.filter(skills, function(s)
      local skill = Fk.skills[s]
      return not current:hasSkill(skill, true) and skill.frequency < 3 and not skill.lordSkill
    end)
    table.insert(skills, "shigongCancel")
    H.removeGeneral(room, player, true)
    local choice = room:askForChoice(current, skills, self.name, "#re_heg__shigong-choice", true)
    if choice == "shigongCancel" then
      room:recover{
        who = player,
        num = math.max(1, 1 - player.hp),
        recoverBy = current,
        skillName = self.name,
      }
    else
      room:handleAddLoseSkills(current, choice, self.name)
      room:recover{
        who = player,
        num = math.max(1, player.maxHp - player.hp),
        recoverBy = current,
        skillName = self.name,
      }
    end
  end,
}

liufuren:addSkill(zhuidu)
liufuren:addSkill(shigong)
liufuren:addCompanions("hs__yuanshao")
Fk:loadTranslationTable{
  ["re_heg__liufuren"] = "刘夫人",
  ["#re_heg__liufuren"] = "酷妒无节",
  ["illustrator:re_heg__liufuren"] = "Jzeo",
  ["designer:re_heg__liufuren"] = "梦魇狂朝",

  ["re_heg__zhuidu"] = "追妒",
  [":re_heg__zhuidu"] = "每回合限一次，当你造成伤害时，你可令受伤角色选择一项：1.令此伤害+1；2.弃置装备区里的的所有牌；若其为女性角色，则你可背水：弃置一张牌。",
  ["#re_heg__zhuidu-discard"] = "追妒：你可以弃置一张牌，然后背水",
  ["zhuidu_add_damage"] = "此伤害+1",
  ["zhuidu_discard"] = "弃置装备区所有牌",
  ["#re_heg__zhuidu-choice"] = "追妒：你须选择一项",
  ["re_heg__shigong"] = "示恭",
  [":re_heg__shigong"] = "限定技，你的回合外，当你进入濒死状态时，你可以移除副将，然后令当前回合角色选择一项：1.获得你移除的副将上一个无技能类型的技能，令你回复体力至体力上限；2. 令你回复体力至1点。",
  ["shigongCancel"] = "取消",
  [":shigongCancel"] = "取消：令刘夫人将体力回复至体力上限",

  ["$re_heg__zhuidu1"] = "到了阴司地府，你们也别想好过！",
  ["$re_heg__zhuidu2"] = "髡头墨面，杀人诛心。",
  ["$re_heg__shigong1"] = "冀州安定，此司空之功也……",
  ["$re_heg__shigong2"] = "妾当自缚，以示诚心。",
  ["~re_heg__liufuren"] = "害人终害己，最毒妇人心……",
}
]]
return extension